package lab4Client;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Client {
	public static Socket s;
	static int end=0;
	 private static void closeSocket() {
	    	end=1;
	    	try {
				s.close();
			} catch (IOException e) {
				System.out.println("error-socket close");
				
			}
	    }
	public static void main(String[] args) {
		 final Frame f=new Frame("Чат");
		 f.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					f.setVisible(false);
					f.dispose();
					try {
					closeSocket();
					} catch (Exception e1) {
						System.out.println("error");
						
					}
				}
					
			});
		 TextArea text=new TextArea(25,60);
		 text.setEditable(false);
		 final TextField t1 = new TextField("Введите имя");
		 final TextField t2 = new TextField("Введите сообщение");
		 Button b = new Button("Отправить");
		 final Panel p = new Panel();
		 p.add(text);
		 final Label l3=new Label("Заполните, пожалуйста, все поля!");
		 l3.setVisible(false);
		 p.add(t1);
		 p.add(t2);
		 p.add(b);
		 p.add(l3);
		 f.setSize(500,500);
		 f.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 250, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 250);
		 f.add(p);
		
	try {
		s = new Socket("localhost", 1999);
		 f.setVisible(true);
		BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
		final PrintWriter output = new PrintWriter(s.getOutputStream(), true);
		b.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				
				String[] mail = new String[2];
				mail[0] = new String(t1.getText());
				mail[1] = new String(t2.getText());
				if((mail[0].length()>0)&&(mail[1].length()>0)) {
					 l3.setVisible(false);
					output.println(mail[0]);
					output.println(mail[1]);
				} else {
					 l3.setVisible(true);
					 f.revalidate();
				}
			}

		});
		String str;
		while(true)
		{
			str=input.readLine();
			text.append(str+'\n');
			str=input.readLine();
			text.append(str+'\n'+'\n');
			
		}
	} catch (UnknownHostException e) {
		f.setVisible(false);
		f.dispose();
		 final Frame f2=new Frame("Ошибка");
		 f2.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e2) {
					f2.setVisible(false);
					f2.dispose();
					try{
					closeSocket();
					} catch (Exception e1) {
						System.out.println("error");
						
					}
				}
					
			});
		 f2.setSize(400,100);
		 f2.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 200, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 50);
		 Label l=new Label("Неизвестный хост!");
		 l.setAlignment(1);
		 f2.add(l);
		 f2.setVisible(true);
	} catch (IOException e) {
		if(end==0){
		f.setVisible(false);
		f.dispose();
		 final Frame f2=new Frame("Ошибка");
		 f2.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e3) {
					f2.setVisible(false);
					f2.dispose();
					try{
					closeSocket();
					} catch (Exception e1) {
						System.out.println("error");
						
					}
				}
					
			});
		 f2.setSize(400,100);
		 f2.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 200, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 50);
		 Label l=new Label("Сервер не доступен!");
		 l.setAlignment(1);
		 f2.add(l);
		 f2.setVisible(true);
		}
	}
	}
}
